cross compiler hello
Install ARM cross-compiler on ubuntu machine and use it's for hello world
Table of Content
A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running (wikipedia)
install cross compiler#
install crosscompiler tools
sudo apt install crossbuild-essential-arm64
toolchain setting file#
aarch64-linux-gnu-toolchain.cmake
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR "aarch64")
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)
set(CMAKE_CUDA_COMPILER nvcc)
set(CMAKE_CUDA_HOST_COMPILER aarch64-linux-gnu-gcc)
set(CMAKE_FIND_ROOT_PATH "/usr/aarch64-linux-gnu")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
demo#
├── aarch64-linux-gnu-toolchain.cmake
├── build
├── CMakeLists.txt
└── src
└── hello.cpp
code#
#include <iostream>
int main(){
std::cout << "hello cross compiler" << std::endl;
return 0;
}
cmake_minimum_required(VERSION 3.15)
# Toolchain settings
set(CMAKE_TOOLCHAIN_FILE aarch64-linux-gnu-toolchain.cmake)
project(CrossCompiler_demo)
add_executable(hello_cc src/hello.cpp)
Warning
It is crucial to set the value of CMAKE_TOOLCHAIN_FILE before project() is invoked, because project() triggers toolchain detection and verification.
cmake gui#
- using cmake gui
- set cross compiler settings from file


make, check, run#
make#
- Run
makefrombuildfolder - Check executable arch with
filecommand - Run with qemu or Copy to embedded device
check#
cd build
# Result
file hello_cc
hello_cc: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, BuildID[sha1]=5425e3fd790ba1a6a07c4963f0606a58edf53aa7, for GNU/Linux 3.7.0, not stripped
run#
Run ARM binary using qemu
sudo apt-get install qemu-user-static
# from build folder
qemu-arm-static -L /usr/arm-linux-gnueabihf ./hello_cc